
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>ThreadLocal - 学习卡片</title>
      <style>
        body { font-family: sans-serif; background-color: #f0f8ff; color: #333; display: flex; flex-direction: column; align-items: center; padding: 50px 20px; }
        .header h1 { font-size: 32px; }
        .grid-container { display: grid; grid-template-columns: repeat(3, 1fr); gap: 28px; width: 100%; max-width: 1200px; }
        .card-container { perspective: 1200px; cursor: pointer; height: 250px; }
        .card { width: 100%; height: 100%; position: relative; transform-style: preserve-3d; transition: transform 0.7s; border-radius: 16px; box-shadow: 0 4px 16px rgba(0,0,0,0.08); }
        .card-container.flipped .card { transform: rotateY(180deg); }
        .card-face { position: absolute; width: 100%; height: 100%; backface-visibility: hidden; display: flex; flex-direction: column; box-sizing: border-box; border-radius: 16px; background-color: #fff; padding: 24px; }
        .card-back { background-color: #f0fff4; transform: rotateY(180deg); justify-content: space-between; }
        .card-category { font-size: 14px; color: #0052d9; margin-bottom: 8px; font-weight: 500; }
        .card-question { font-size: 20px; font-weight: 500; flex-grow: 1; display: flex; align-items: center; justify-content: center; text-align: center; }
        .card-answer-wrapper { flex-grow: 1; overflow-y: auto; }
        .card-answer { font-size: 15px; line-height: 1.7; }
        .card-footer { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 16px; margin-top: 16px; }
        .card-source { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 12px; margin-top: 12px; }
      </style>
    </head>
    <body>
      <div class="header">
        <h1>ThreadLocal - 学习卡片</h1>
      </div>
      <div class="grid-container">
        
    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">理论</div>
          <div class="card-question">ThreadLocal的核心设计理念是什么？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">理论</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">ThreadLocal的设计初衷是为每个线程提供一个独立的变量副本。它就像是为每个线程提供了独立的存储空间或“私人订制”的变量，使得每个线程都可以拥有自己的数据而不必担心被其他线程干扰。</div>
          </div>
          <div class="card-source">来源: ThreadLocal的基本概念</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">ThreadLocal是如何在内部实现为每个线程存储独立变量的？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">ThreadLocal的实现依赖于一个名为ThreadLocalMap的内部数据结构。这个Map负责管理每个线程的变量副本，其中键是ThreadLocal实例，值是具体的变量值。</div>
          </div>
          <div class="card-source">来源: ThreadLocalMap</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">技术</div>
          <div class="card-question">在使用ThreadLocal时，为什么需要特别关注内存管理？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">技术</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">因为在多线程环境中，线程的生命周期通常比ThreadLocal变量的生命周期长。如果线程结束时不清理不再使用的ThreadLocal变量，这些变量将无法被回收，从而导致内存泄漏，尤其是在长期运行的服务中。</div>
          </div>
          <div class="card-source">来源: 内存管理</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">特性</div>
          <div class="card-question">ThreadLocal中的initialValue()方法有什么作用？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">特性</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">initialValue()方法用于在每个线程第一次通过get()方法访问ThreadLocal变量且没有set过值时，为其提供一个默认的初始值。该方法默认返回null，但可以被子类重写以提供特定的初始值。</div>
          </div>
          <div class="card-source">来源: initialValue()方法</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">模式</div>
          <div class="card-question">请列举几个ThreadLocal的典型应用场景。</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">模式</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">ThreadLocal非常适合需要线程隔离的场景，典型的应用包括：1. 数据库连接池，确保每个线程拥有独立的数据库连接；2. 会话管理，使每个线程可以保存独立的会话信息；3. 缓存，为每个线程提供自己的缓存以避免竞争。</div>
          </div>
          <div class="card-source">来源: ThreadLocal的应用场景</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">ThreadLocal的set(T value)方法是如何工作的？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">set(T value)方法首先获取当前线程（Thread.currentThread()），然后获取该线程内部的ThreadLocalMap。如果Map存在，就将当前的ThreadLocal实例作为键，传入的value作为值存入Map中；如果Map不存在，则为该线程创建一个新的Map并存入初始键值对。</div>
          </div>
          <div class="card-source">来源: get()和set()方法</div>
        </div>
      </div>
    </div>

      </div>
    </body>
    </html>
